package com.yulebaby.callsenter.ui.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.yulebaby.callsenter.R;
import com.yulebaby.callsenter.adapter.PreSaleRemarkAdapter;
import com.yulebaby.callsenter.bean.CustomerBean;
import com.yulebaby.callsenter.bean.PreSaleRemarkBean;
import com.yulebaby.callsenter.global.Constant;
import com.yulebaby.callsenter.http.callback.HttpCallback;
import com.yulebaby.callsenter.task.RequestPreSaleRemarkTask;
import com.yulebaby.callsenter.ui.activity.LookOverTrackProgressActivity;
import com.yulebaby.callsenter.ui.activity.PreSaleRemarkAcitivity;
import com.yulebaby.callsenter.utils.ComLogUtils;
import com.yulebaby.callsenter.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author YK
 * @date 2016-12-22.
 */

public class CustomPopwindow extends PopupWindow {

    private View mView;
    private ArrayList<PreSaleRemarkBean> mPreSaleRemarkList = new ArrayList<>();
    private CustomerBean mPopBean;
    private Activity mContext;
    private PreSaleRemarkAdapter popAdapter;
    private ProgressBar mPbShowProgress;
    private RecyclerView popRecyclerView;
    private RelativeLayout mFlFail;
    private LinearLayout mLlShowresult;
    private ImageButton imageButton;
    private TextView tv_size_null;

    public CustomPopwindow(Activity cont, View.OnClickListener itemsOnClick, CustomerBean PopBean) {
        super(cont);
        mPopBean = PopBean;
        mContext = cont;
        initView(itemsOnClick);
        initData();

    }

    private void initData() {
        if (mPreSaleRemarkList.size() == 0) {
            requestDataPop(mPopBean);
        } else {
            popAdapter.notifyDataSetChanged();
        }
        popRecyclerView.setHasFixedSize(true);
        popRecyclerView.setLayoutManager(new YuLeBaByLinearLayoutManager(mContext));
        popRecyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                PreSaleRemarkBean bean = (PreSaleRemarkBean) baseQuickAdapter.getItem(position);
                switch (view.getId()) {
                    case R.id.rl_trackprogress:
                        if (bean.getTrack_pro() > 3) {
                            Intent intent = new Intent(mContext, LookOverTrackProgressActivity.class);
                            intent.putExtra(Constant.PRE_VISIT_TIMES, bean.getVisit_times());
                            intent.putExtra(Constant.CUSTOMER_ID, bean.getCustomer_id());
                            mContext.startActivity(intent);
                        }
                        break;
                }
            }
        });
        popAdapter = new PreSaleRemarkAdapter(mPreSaleRemarkList);
        popRecyclerView.setAdapter(popAdapter);
    }

    private void initView(View.OnClickListener itemsOnClick) {
        LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = mInflater.inflate(R.layout.activity_pre_sale_remark_popupwindow, null);

        popRecyclerView = (RecyclerView) mView.findViewById(R.id.rv_showpresale);
        mPbShowProgress = (ProgressBar) mView.findViewById(R.id.pb_showProgress);
        mFlFail = (RelativeLayout) mView.findViewById(R.id.fl_fail);
        mLlShowresult = (LinearLayout) mView.findViewById(R.id.ll_showresult);
        imageButton = (ImageButton) mView.findViewById(R.id.ib_cancal);
        tv_size_null = (TextView) mView.findViewById(R.id.tv_size_null);

        String customer_name = mPopBean.getCustomer_name();
        TextView tv_username = (TextView) mView.findViewById(R.id.tv_username);
        tv_username.setText(customer_name);

        //取消按钮
        imageButton.setOnClickListener(v -> {
            //销毁弹出框
            dismiss();
            backgroundAlpha(mContext, 1f);
        });
        //设置按钮监听
        imageButton.setOnClickListener(itemsOnClick);
        //设置SelectPicPopupWindow的View
        this.setContentView(mView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(1250);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置PopupWindow可触摸
        this.setTouchable(true);
        //设置非PopupWindow区域是否可触摸
        this.setOutsideTouchable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.select_animpop);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xffffffff);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        backgroundAlpha(mContext, 0.5f);//0.0-1.0
        this.setOnDismissListener(() -> backgroundAlpha(mContext, 1f));
        // 这里如果返回true的话，touch事件将被拦截,手机屏幕都无法点击
        // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
        this.setTouchInterceptor((View v, MotionEvent event) -> false);
    }

    /**
     * pop请求数据
     *
     * @param customerBean
     */
    private void requestDataPop(CustomerBean customerBean) {
        showView(mPbShowProgress);
        if (customerBean != null) {
            RequestPreSaleRemarkTask.doRequestPreSaleRemarkTask(mContext, customerBean, new HttpCallback<List<PreSaleRemarkBean>>() {
                @Override
                public void onSuccess(List<PreSaleRemarkBean> result) {
                    if (result != null) {
                        processResultPop(result);
                    }
                }

                @Override
                public void onFail(String exception) {
                    super.onFail(exception);
                    showView(mFlFail); //显示失败界面
                    ToastUtil.showToast("查看跟踪记录请求失败");
                }

                @Override
                public void onFailParam(String info) {
                    processResultPop(null);
                }
            });
        } else {
            showView(mFlFail);
        }
    }

    /**
     * pop对结果进行处理
     *
     * @param result
     */
    private void processResultPop(List<PreSaleRemarkBean> result) {
        mPreSaleRemarkList.clear();
        popAdapter.notifyDataSetChanged();
        if (result != null) {
            mPreSaleRemarkList.addAll(result);
            showView(mLlShowresult);
        } else {
            showView(tv_size_null);
        }
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }

    /**
     * 显示View
     */
    private void showView(View v) {
        hideAllView();
        v.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏所有的View
     */
    private void hideAllView() {
        mPbShowProgress.setVisibility(View.INVISIBLE);
        mFlFail.setVisibility(View.INVISIBLE);
        mLlShowresult.setVisibility(View.INVISIBLE);
        tv_size_null.setVisibility(View.INVISIBLE);
    }
}
